package net.wangds.program.auth.module;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import net.wangds.auth.module.UserStatusEnum;
import net.wangds.auth.module.impls.defaults.DefaultAccount;
import net.wangds.log.helper.LogHelper;
import net.wangds.utils.JsonUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Entity
@Table(name="pro_auth_account")
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuthAccount extends DefaultAccount implements UserDetails {
    private static final long serialVersionUID = 0;


    /**
     * 新普通账户.
     * @param loginName 登录名.
     * @param password 加密后的密码.
     * @param deviceId 设备id.
     * @param dispName 显示名称,真实姓名.
     * @param nickname 昵称.
     * @param desc 个人描述.
     * @param avatar 头型资源id.
     * @param referee 推荐人.
     * @return 新账户.
     */
    public static AuthAccount ofNewUser(String loginName, String password, String deviceId,
                                        String dispName, String nickname, String desc,
                                        String avatar, String referee) {
        AuthAccount acc = new AuthAccount();
        acc.setDeviceId(deviceId);
        acc.setDisplayName(dispName);
        acc.setDescription(desc);
        acc.setReferee(referee);
        acc.setLoginname(loginName);
        acc.setNickname(nickname);
        acc.setAvatar(avatar);
        acc.setPassword(password);
        LogHelper.dev("创建普通用户："+ JsonUtils.toJsonStr(acc));
        return acc;
    }

    /**
     * 新普通账户.
     * @param account 请求.
     * @return 普通账户.
     */
    public static AuthAccount ofNewUser(AuthAccount account) {
        AuthAccount acc = new AuthAccount();
        acc.setId(account.getId());
        acc.setWxOpenId(account.getWxOpenId());
        acc.setWxUnionId(account.getWxUnionId());
        acc.setDeviceId(acc.getDeviceId());
        acc.setDisplayName(account.getDisplayName());
        acc.setDescription(account.getDescription());
        acc.setReferee(account.getReferee());
        acc.setLoginname(account.getLoginname());
        acc.setNickname(account.getNickname());
        acc.setAvatar(account.getAvatar());
        acc.setPassword(account.getPassword());
        LogHelper.dev("创建普通用户："+ JsonUtils.toJsonStr(acc));
        return acc;
    }

    @Getter
    @Setter
    private String deviceId;

    @Getter
    @Setter
    private String tel;

    @Getter
    @Setter
    private String description;

    /**
     * 积分.
     */
    @Getter
    @Setter
    private long score;

    /**
     * 推荐人.
     */
    @Getter
    @Setter
    private String referee;

    @Getter
    @Setter
    @Transient
    private String sessionKey;

    @ApiModelProperty("头像文件id")
    @Getter
    @Setter
    private String iconId;

    @ApiModelProperty(value = "排序权重")
    @Getter
    @Setter
    @Column()
    private long orderWeight;

    @ApiModelProperty(value = "vip级别")
    @Getter
    @Setter
    @Column(length=36)
    private String vipLevel;

    @ApiModelProperty(value = "vip级别")
    @Getter
    @Setter
    @Column(length=50)
    private String vipLabel;

    @ApiModelProperty(value = "账户类型")
    @Getter
    @Setter
    @Column(length=50)
    private int cata;

    @ApiModelProperty(value = "账户类型")
    @Getter
    @Setter
    @Column(length=50)
    private String cataLabel;


    @Transient
    private final List<GrantedAuthority> authorities = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getUsername() {
        return this.getLoginname();
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.getStatus()== UserStatusEnum.NORMAL;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.getStatus()==UserStatusEnum.UNCERTIFIED||this.getStatus()==UserStatusEnum.NORMAL;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return isAccountNonLocked();
    }

    @Override
    public boolean isEnabled() {
        return isAccountNonExpired();
    }


    public AuthAccount toMini(){
        AuthAccount res = new AuthAccount();
        res.setId(this.getId());
        res.setNickname(this.getNickname());
        res.setLoginname(this.getLoginname());
        res.setIconId(this.getIconId());
        return res;
    }
}
